{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Basics of Python: Conditionals </h2>\n",
    "\n",
    "We review using conditionals in python here. \n",
    "\n",
    "Please run each cell and check the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's randomly pick a number between 0 and 9, and print its value if it is greater than 5\n",
    "\n",
    "from random import randrange\n",
    "r = randrange(10)\n",
    "\n",
    "if r > 5: print(r) # when the condition (r > 5) is valid/true, the code (print(r)) will be executed\n",
    "# you may need to execute your code more than once to observe an outcome"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# repeat the same task four times, and also print the value of iteration variable (i) \n",
    "\n",
    "for i in range(4):\n",
    "    r = randrange(10) # this code belongs to for-loop, and so it is indented\n",
    "    if r > 5: # this code also belongs to for-loop, and so it is indented as well\n",
    "        print(\"i=\",i,\"r=\",r) # this code belongs to if-statement, and so it is indented with respect to if-statement\n",
    "\n",
    "# if you are unlucky (with probabability less than 13/100), you may still do not see any outcome in a single run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# do the same task 100 times, and find the percentage of successful iteration (attempt)\n",
    "# an iteration (attempt) is successful if the randomly picked number is greater than 5\n",
    "# the expected percentage is 40, because, out of 10 numbers, there are 4 numbers greater than 5 \n",
    "#     but the experimental results differ\n",
    "\n",
    "success = 0\n",
    "for i in range(100):\n",
    "    r = randrange(10)\n",
    "    if r > 5: \n",
    "        success = success + 1\n",
    "print(success,\"%\")\n",
    "\n",
    "# each run most probabily will give different percentage value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's randomly pick a number between 0 and 9, and says whether it less than 6 or not\n",
    "# we use two conditionals here\n",
    "\n",
    "r = randrange(10)\n",
    "print(\"the picked number is \",r)\n",
    "\n",
    "if r < 6:\n",
    "    print(\"it is less than 6\")\n",
    "if r >= 6:\n",
    "    print(\"it is greater than or equal to 6\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's write the same code by using if-else\n",
    "\n",
    "r = randrange(10)\n",
    "print(\"the picked number is \",r)\n",
    "\n",
    "if r < 6:\n",
    "    print(\"it is less than 6\")\n",
    "else: # if the above condition (r<6) is False\n",
    "    print(\"it is greater than or equal to 6\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "Randomly pick a number between 10 and 50, and say whether it is even or not.\n",
    "\n",
    "Remember the reminder/mod operator (%). \n",
    "\n",
    "A number is even if is exactly divided by 2. That is, the reminder is zero when divided by 2 over integers.\n",
    "\n",
    "If the number is odd, then the reminder will be 1 when divided by 2 over integers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B12_Python_Basics_Conditionals_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# when there are many conditionals, we can use if-elif-else\n",
    "#\n",
    "\n",
    "# let's randomly pick an even number between 1 and 99\n",
    "# then determine whether it is less than 25, between 25 and 50, between 51 and 75, or greater than 75.\n",
    "\n",
    "r = randrange(2,100,2) # randonmly pick a number in range {2,4,6,...,98}, which satisfies our condition\n",
    "# let's print this range to verify our claim\n",
    "print(list(range(2,100,2)))\n",
    "\n",
    "print() # print an empty line\n",
    "print(\"the picked number is\",r)\n",
    "\n",
    "if r < 25:\n",
    "    print(\"it is less than 25\")\n",
    "elif r<=50: # if the above condition is False and my condition is True\n",
    "    print(\"it is between 25 and 50\")\n",
    "elif r<=75: # if both conditions above are False and my condition is True\n",
    "    print(\"it is between 51 and 75\")\n",
    "else: # if all conditions above are False\n",
    "    print(\"it is greater than 75\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "Randomly pick a number between 0 and 99. \n",
    "\n",
    "With half probability $\\left( 0.5 = \\dfrac{1}{2} \\right)$, it is expected to be between 0 and 49 or between 50 and 99.\n",
    "\n",
    "Let's try to calculate this probability experimentially.\n",
    "\n",
    "N=100 times randomly pick a number between 0 and 99, calculate the frequencies of both cases, and then divide each by 100. \n",
    "\n",
    "Repeat the same experiment for N=1,000, N=10,000, and N=100,000.\n",
    "\n",
    "Experimental results should get closer to the ideal ratio when N increases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B12_Python_Basics_Conditionals_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Prime numbers</h3>\n",
    "\n",
    "A number is prime if it greater than 1 and it is divided by only 1 and itself.\n",
    "\n",
    "For example, any negative number, 0, or 1 is not prime.\n",
    "\n",
    "2, 3, 5, 7, and 11 are the first five prime numbers. \n",
    "\n",
    "31 is another prime number because it cannot be divided by any natural number other than 1 and 31. \n",
    "<br>&nbsp;&nbsp;&nbsp;&nbsp;\n",
    "In other words, any number in {2,3,4,...,30} does not divide 31. \n",
    "\n",
    "4 is not a prime number because it is divided by 2. The reminder is 0 when divided by 2.\n",
    "<br>\n",
    "9 is not a prime number because it is divided by 3. The reminder is 0 when divided by 3.\n",
    "<br>\n",
    "15 is not a prime number because it is divided by 3 or 5. The reminder is 0 when divided by 3 or 5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's determine whether a randomly picked number between -10 and 100 is prime or not.\n",
    "# this is a good example for using more than one conditionals in different parts of the program\n",
    "# this is also an example for \"break\" command, which terminates any loop immediately\n",
    "\n",
    "r = randrange(-10,101) # pick a number between -10 and 100\n",
    "print(r) # print is value\n",
    "\n",
    "if r < 2: print(\"it is NOT a prime number\") # this is by definition\n",
    "elif r == 2: print(\"it is a PRIME number\") # we already know this\n",
    "else:\n",
    "    prime=True # we assume that r is prime, and try to falsify this assumption by looking for a divisor in the following loop\n",
    "    \n",
    "    for i in range(2,r): # check all integers between 2 and r-1\n",
    "        if r % i ==0: # if i divides r without any reminder (or reminder is zero), then r is not be a prime number\n",
    "            print(\"it is NOT a prime number\")\n",
    "            prime=False # our assumption is falsifed\n",
    "            break # TERMINATE the iteration immediately\n",
    "        # we are out of if-scope\n",
    "    # we are out of for-loop-scope\n",
    "    if prime == True: # if our assumption is still True (if it was not falsied inside for-loop)\n",
    "        print(\"it is a PRIME number\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Writing function in python </h3>\n",
    "\n",
    "Our code determines whether a given integer is prime or not.\n",
    "\n",
    "We can store it and use it later.\n",
    "\n",
    "We can write it as function (module), and then call it whenever it is needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this is an example to write a function\n",
    "# our function will return a Boolean value True or False\n",
    "\n",
    "def prime(number): # our function has one parameter\n",
    "    if number < 2: return False # once return command is executed, we exit the function\n",
    "    if number == 2: return True # because of return command, we can use \"if\" instead of \"elif\"\n",
    "    if number % 2 == 0: return False # any even number greater than 2 is not prime, because it is divisible by 2\n",
    "    for i in range(3,number,2): # we can skip even integers\n",
    "        if number % i == 0: return False # once we find a divisor of the number, we return False and exit the function\n",
    "    return True # the number has passed all checks until now\n",
    "    # because of return command, the program can be shorten\n",
    "    #     remark that this might not be a good choice everytime for readibility of codes \n",
    "\n",
    "# let's test our program by printing all prime numbers between -10 and 30\n",
    "for i in range(-10,30):\n",
    "    # we pass i to the function prime\n",
    "    if prime(i): # the function prime(i) returns True or False\n",
    "        print(i) # this code will be executed if i is prime, i.e., prime(i) returns True"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
